package com.baidu.health.service;

import com.baidu.health.mapper.TbArticleMapper;
import com.baidu.health.pojo.TbArticle;
import com.baidu.health.pojo.resultVo.articleVo;
import com.baidu.health.pojo.resultVo.resultVo;
import com.baidu.health.utils.KeyWordFilter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.health.pojo.TbComment;
import com.baidu.health.mapper.TbCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbCommentServiceImpl extends ServiceImpl<TbCommentMapper, TbComment>{

    @Autowired
    TbCommentMapper commentMapper;

    @Autowired
    TbArticleMapper articleMapper;

    public resultVo commentArticle(TbComment comment) throws Exception {

        KeyWordFilter keyWordFilter = new KeyWordFilter();

        boolean checkContent = keyWordFilter.doFilter(comment.getCommentContent());

        if ( checkContent ){
            commentMapper.insert(comment);
            TbArticle article = articleMapper.selectOne(new QueryWrapper<TbArticle>().lambda().eq(TbArticle::getArticleId, comment.getArticleId()));
            article.setArticleCommentNums(article.getArticleCommentNums() + 1);
            articleMapper.updateById(article);
            return new resultVo(200,"评论成功",null);
        }else {
            comment.setCheckFlag(2);
            comment.setCommentContent(comment.getCommentContent()+"(包含敏感词语)");
            commentMapper.insert(comment);
            return new resultVo(500,"该评论中包含敏感词语，不予评论",null);
        }


    }

    public resultVo getChildCommentRecord(Integer articleId, Integer fatherId) {

        QueryWrapper<TbComment> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(TbComment::getArticleId,articleId);
        wrapper.lambda().eq(TbComment::getFatherId,fatherId);
        wrapper.lambda().eq(TbComment::getCheckFlag,1);

        List<TbComment> comments = commentMapper.selectList(wrapper);

        return new resultVo(200,"查询成功",comments);

    }

    public articleVo unreviewedCommentList(Integer pageNum, Integer pageSize) {
        Page<TbComment> page = new Page<>(pageNum,pageSize);
        List<TbComment> comments = commentMapper.selectPage(page, null).getRecords();
        int size = this.unreviewedCommentListNums();
        return new articleVo(true,200,"获取成功",comments,size);
    }
    public Integer unreviewedCommentListNums(){
        return commentMapper.selectList(new QueryWrapper<TbComment>()).size();
    }

    public resultVo checkComment(Integer checkStatus, TbComment comment) {
        comment.setUpdateTime(new Date());
        comment.setCheckFlag(checkStatus);
        commentMapper.updateById(comment);
        return new resultVo(200,"审核完成",null);
    }
}




